package com.example.SpringModelManage.controller;

import com.example.SpringModelManage.entity.UpdateModelDataset;
import com.example.SpringModelManage.service.ModelService;
import com.example.SpringModelManage.util.JsonResult;
import com.example.SpringModelManage.util.ModelUtil;
import com.example.SpringModelManage.util.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Class Description
 *
 * @author hulilei
 * @version 1.0.0
 * @date 2021/10/24 16:12
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/model")
@ResponseBody
public class ModelController {
    @Autowired
    private ModelService modelService;

    @Autowired
    private ModelUtil modelUtil;

    @GetMapping("/testmodel")
    public String testModel() {
        return "Test Model Service";
    }

    /**模型id传参*/
    private static String ModelID;

    /**
     * 查询模型信息（根据模型ID）
     */
    @GetMapping("/selectModelById")
    public JsonResult selectModelById(@RequestParam("modelId") String modelId) {
        if (modelId.isBlank() || modelId.isEmpty()) {
            return new JsonResult(StatusCode.INFO_ERROR.getCode(), "参数为空");
        }
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功", modelService.selectModelById(modelId));
    }

    /**
     * 分页展示模型信息
     * @startIndex：开始的索引（从0开始）
     * @pageSize：页面的大小
     */
    @GetMapping("/getModelByLimit")
    public JsonResult getModelByLimit(
            @Validated @RequestParam("developId") String developId,
            @Validated @RequestParam("startIndex") Integer startIndex,
            @Validated @RequestParam("pageSize") Integer pageSize
    ) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功",
                modelService.getModelByLimit(developId, startIndex, pageSize)
        );
    }

    /**
     * 根据各项条件，筛选模型信息
     */
    @GetMapping("/getModelByLimitSearch")
    public JsonResult getModelByLimitSearch(
            @Validated @RequestParam("developId") String developId,
            @RequestParam("modelId") String modelId,
            @RequestParam("modelName") String modelName,
            @RequestParam("modelVersion") String modelVersion,
            @RequestParam("modelType") String modelType,
            @RequestParam("modelStatus") String modelStatus,
            @Validated @RequestParam("startIndex") Integer startIndex,
            @Validated @RequestParam("pageSize") Integer pageSize
    ) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功",
                modelService.getModelByLimitSearch(developId, modelId, modelName,
                        modelVersion, modelType, modelStatus, startIndex, pageSize
                )
        );
    }

    /**
     * 创建模型
     */
    @PostMapping("/createModel")
    public JsonResult createModel(
            @Validated @RequestParam("developId") String developId,
            @Validated @RequestParam("modelName") String modelName,
            @Validated @RequestParam("modelType") String modelType,
            @Validated @RequestParam("modelLanguage") String modelLanguage,
            @Validated @RequestParam("modelDescribe") String modelDescribe,
            @Validated @RequestParam("modelCreateTime") String modelCreateTime
    ) {
        String modelId = modelUtil.createMdId();
        ModelID = modelId;
        String umId = modelUtil.createMdId();
        String modelVersion = "V1.0";
        String modelStatus = "已创建";
        modelService.createModel(developId,modelId, umId, modelName, modelType,
                modelLanguage, modelDescribe, modelCreateTime, modelVersion, modelStatus
        );
        // 响应modelId给前端
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功", modelId);
    }

    /**不同微服务之间参数调用
     * 给examine中的Listener传参*/
    @GetMapping("/getModelId")
    public String getModelId() {
        return ModelID;
    }

    /**
     * 根据开发者ID，获取模型ID和名称
     *
     * @modelStatus：模型状态
     */
    @GetMapping("/getModelNameByDevelopId")
    public JsonResult getModelNameByDevelopId(
            @Validated @RequestParam("developId") String developId,
            @Validated @RequestParam("modelStatus") String modelStatus
    ) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功", modelService.getModelNameByDevelopId(developId, modelStatus));
    }

    /**
     * 上传模型文件
     */

    /**
     * 上传模型依赖文件
     */

    /**
     * 上传模型功能说明书
     */

    /**
     * 上传模型的操作
     */
    @PostMapping("/uploadModel")
    public JsonResult uploadModel(
        @Validated @RequestParam("modelId")String modelId,
        @Validated @RequestParam("datasetId") String datasetId,
        @Validated @RequestParam("modelFilePath") String modelFilePath,
        @Validated @RequestParam("modelRequirementPath") String modelRequirementPath,
        @Validated @RequestParam("modelSpecificationPath") String modelSpecificationPath,
        @Validated @RequestParam("modelUploadTime") String modelUploadTime
    ) {
        String modelStatus = "已上传";
        String mdId = modelUtil.createMdId();
        modelService.uploadModel(modelId, datasetId, mdId, modelStatus, modelFilePath, modelRequirementPath, modelSpecificationPath, modelUploadTime);
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功");
    }

    /**
     * 获取模型指导价（根据模型ID）
     */
    @GetMapping("/getModelGuidePrice")
    public JsonResult getModelGuidePrice(@Validated @RequestParam("modelId") String modelId) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功", modelService.getModelGuidePrice(modelId));
    }

    /**
     * 发布模型
     */
    @PostMapping("/releaseModel")
    public JsonResult releaseModel(
            @Validated @RequestParam("modelId") String modelId,
            @Validated @RequestParam("modelPrice") String modelPrice,
            @Validated @RequestParam("modelReleaseTime") String modelReleaseTime
    ) {
        String modelStatus = "已发布";
        modelService.releaseModel(modelId, modelPrice, modelStatus, modelReleaseTime);
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功");
    }

    /**
     * 根据模型ID，获取模型版本
     */
    @GetMapping("/getModelVersionById")
    public JsonResult getModelVersionById(@Validated @RequestParam("modelId") String modelId) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功", modelService.getModelVersionById(modelId));
    }

    /**
     * 更新模型文件的操作
     */
    @PostMapping("/updateModelFile")
    public JsonResult updateModelFile(
            @Validated @RequestParam("developId") String developId,
            @Validated @RequestParam("modelId") String modelId,
            @Validated @RequestParam("modelVersion") String modelVersion,
            @Validated @RequestParam("modelFilePath") String modelFilePath
    ) {
        modelService.updateModelFile(developId, modelId, modelVersion, modelFilePath);
        // 缺少删除旧模型文件的操作
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功");
    }

    /**
     * 根据模型ID，获取模型的数据集名称
     */
    @GetMapping("/getDatasetNameByModelId")
    public JsonResult getDatasetNameByModelId(
            @Validated @RequestParam("modelId") String modelId
    ) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功",
                modelService.getDatasetNameByModelId(modelId));
    }

    /**
     * 根据模型ID，获取模型的数据集ID和名称
     */
    @GetMapping("/getModelDatasetByModelId")
    public JsonResult getModelDatasetByModelId(
            @Validated @RequestParam("modelId") String modelId
    ) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功",
                modelService.getModelDatasetByModelId(modelId)
        );
    }

    /**
     * 更新模型数据集(的绑定关系)
     */
    @PostMapping("/updateModelDataset")
    public JsonResult updateModelDataset(
            @RequestBody UpdateModelDataset updateModelDataset
            /**
             @Validated @RequestParam("modelId") String modelId,
             @Validated @RequestParam("modelUpdateTime") String modelUpdateTime,
             @Validated @RequestParam("datasetIdOld") List<String> datasetIdOld,
             @Validated @RequestParam("datasetIdNew") List<String> datasetIdNew
             */
    ) {
        List<String> datasetAdd = modelUtil.getAddModelDataset(updateModelDataset.getDatasetIdOld(), updateModelDataset.getDatasetIdNew());
        List<String> datasetDelete = modelUtil.getDeleteModelDataset(updateModelDataset.getDatasetIdOld(), updateModelDataset.getDatasetIdNew());
        // 添加模型新的数据集
        for (String item : datasetAdd) {
            modelService.updateModelDataset(
                    updateModelDataset.getModelId(),
                    updateModelDataset.getModelUpdateTime(),
                    "add",
                    item,
                    modelUtil.createMdId()
            );
        }
        // 删除模型中旧的数据集
        for (String item : datasetDelete) {
            modelService.updateModelDataset(
                    updateModelDataset.getModelId(),
                    updateModelDataset.getModelUpdateTime(),
                    "delete",
                    item,
                    ""
            );
        }
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功");
    }

    /**
     * 获取所有的数据集（根据开发者ID）
     */
    @GetMapping("/getDatasetById")
    public JsonResult getDatasetById(
            @Validated @RequestParam("developId") String developId
    ) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功",
                modelService.getDatasetById(developId)
        );
    }

    /**
     * 获取数据集（根据开发者ID和筛选条件）
     */
    @GetMapping("/getDatasetByLimitSearch")
    public JsonResult getDatasetByLimitSearch(
            @Validated @RequestParam("developId") String developId,
            @RequestParam("datasetId") String datasetId,
            @RequestParam("datasetName") String datasetName,
            @RequestParam("datasetType") String datasetType,
            @RequestParam("datasetVersion") String datasetVersion,
            @Validated @RequestParam("startIndex") Integer startIndex,
            @Validated @RequestParam("pageSize") Integer pageSize
    ) {
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功",
                modelService.getDatasetByLimitSearch(developId, datasetId, datasetName, datasetType,
                        datasetVersion, startIndex, pageSize));
    }

    /**
     * 上传数据集文件
     */


    /**
     * 上传数据集的操作
     */
    @PostMapping("/uploadDataset")
    public JsonResult uploadDataset(
            @Validated @RequestParam("developId")String developId,
            @Validated @RequestParam("datasetName") String datasetName,
            @Validated @RequestParam("datasetUploadTime") String datasetUploadTime,
            @Validated @RequestParam("datasetType") String datasetType,
            @Validated @RequestParam("datasetFilePath") String datasetFilePath
    ) {
        String datasetId = modelUtil.createMdId();
        String datasetVersion = "V1.0";
        modelService.uploadDataset(developId, datasetId, datasetName, datasetVersion, datasetUploadTime, datasetType, datasetFilePath);
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功", datasetId);
    }

    /**
     * 更新数据集的操作
     */
    @PostMapping("/updateDataset")
    public JsonResult updateDataset(
            @Validated @RequestParam("datasetId") String datasetId,
            @Validated @RequestParam("datasetFilePath") String datasetFilePath,
            @Validated @RequestParam("datasetUpdateTime") String datasetUpdateTime,
            @Validated @RequestParam("datasetVersion") String datasetVerison
    ) {
        modelService.updateDataset(datasetId, datasetFilePath, datasetUpdateTime, datasetVerison);
        return new JsonResult(StatusCode.SUCCESS.getCode(), "成功");
    }

}
